#!/bin/bash

echo "===================================="
echo "Host A - Labgrid 服务启动脚本"
echo "===================================="
echo ""

# 检查配置文件
if [ ! -f "exporter.yaml" ]; then
    echo "✗ exporter.yaml 不存在！"
    echo "请确保当前目录下有exporter.yaml文件"
    exit 1
fi

echo "[1/4] 停止现有服务..."
pkill -f "labgrid-coordinator" 2>/dev/null
pkill -f "labgrid-exporter" 2>/dev/null
sleep 2
echo "✓ 现有服务已停止"
echo ""

echo "[2/4] 启动Coordinator..."
labgrid-coordinator -l 0.0.0.0:20408 &
COORD_PID=$!
sleep 3

if ps -p $COORD_PID > /dev/null; then
    echo "✓ Coordinator 运行中 (PID: $COORD_PID)"
else
    echo "✗ Coordinator 启动失败"
    exit 1
fi
echo ""

echo "[3/4] 启动Exporter..."
echo "配置文件: exporter.yaml"
cat exporter.yaml
echo ""

labgrid-exporter -c 127.0.0.1:20408 exporter.yaml &
EXPORT_PID=$!
sleep 3

if ps -p $EXPORT_PID > /dev/null; then
    echo "✓ Exporter 运行中 (PID: $EXPORT_PID)"
else
    echo "✗ Exporter 启动失败"
    kill $COORD_PID 2>/dev/null
    exit 1
fi
echo ""

echo "[4/4] 服务状态检查..."
echo "✓ Coordinator PID: $COORD_PID"
echo "✓ Exporter PID: $EXPORT_PID"
echo ""
echo "===================================="
echo "服务启动完成！"
echo "===================================="
echo ""
echo "要停止服务，请执行："
echo "kill $COORD_PID $EXPORT_PID"

